﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MIS
{
    public partial class login : Form
    {
        string strCon = @"data source=DESKTOP-4ULJ8AF;initial catalog = 'MIS';integrated security =true";
        public login()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void placeHolderTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void placeHolderTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void placeHolderTextBox2_TextChanged_1(object sender, EventArgs e)
        {
            if (password.Text.Length > 0&&password.Text!="请输入密码")
            {
                password.PasswordChar = '*';
            }
            else
            {
                password.PasswordChar = '\0';
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
                checkBox2.Checked = false;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
                checkBox1.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(number.Text))
            {
                MessageBox.Show("请输入账号！");
            }
            else if (string.IsNullOrEmpty(password.Text))
            {
                MessageBox.Show("请输入密码！");
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == false)
            {
                MessageBox.Show("请勾选用户身份");
            }
            else
            {
                using (SqlConnection con = new SqlConnection(strCon))
                {
                    con.Open();
                    if (checkBox1.Checked == true)
                    {
                        MessageBox.Show("医生登录");
                    }
                    else if (checkBox2.Checked == true)
                    {
                        string strCmd = "select * from Plogin where Card_num='{0}' and Password='{1}'";
                        strCmd = string.Format(strCmd, number.Text, password.Text);
                        SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
                        DataSet ds = new DataSet();
                        int m = da.Fill(ds);

                        if (m != 0)
                        {
                            this.Tag = number.Text;
                            register patient = new register(this);
                            patient.Show();
                        }
                        else if (m == 0)
                        {
                            MessageBox.Show("账号或密码错误！");
                        }
                    }
                }
            }

        }



        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Pname.Text))
            {
                MessageBox.Show("请输入姓名");
            }
            else if (string.IsNullOrEmpty(Psex.Text))
            {
                MessageBox.Show("请选择性别");
            }
            else if (string.IsNullOrEmpty(Page.Text))
            {
                MessageBox.Show("请输入年龄");
            }
            else if (string.IsNullOrEmpty(Pphone.Text))
            {
                MessageBox.Show("请输入联系电话");
            }
            else if (string.IsNullOrEmpty(Idtype.Text))
            {
                MessageBox.Show("请选择身份证类型");
            }
            else if (string.IsNullOrEmpty(Idnum.Text))
            {
                MessageBox.Show("请输入身份证号码");
            }
            else if (string.IsNullOrEmpty(character.Text))
            {
                MessageBox.Show("请选择用户身份");
            }
            else if (string.IsNullOrEmpty(regPassword.Text))
            {
                MessageBox.Show("请输入密码！");
            }
            else if(character.Text.ToString()=="医生"){
                if (string.IsNullOrEmpty(Dept_type.Text))
                {
                    MessageBox.Show("请选择科室");
                }
                else if (string.IsNullOrEmpty(Dept.Text))
                {
                    MessageBox.Show("请输入科室号");
                }
                else
                {
                    using (SqlConnection con = new SqlConnection(strCon))
                    {
                        con.Open();

                        string strCmd = "insert Doctor values('{0}','{1}',' ','{2}','{3}','{4}','{5}','{6}','5') " +
                        "insert Dlogin values('{0}','{7}')";
                        strCmd = string.Format(strCmd, Id.Text, Pname.Text, Dept_type.Text, Dept.Text, Psex.Text, Page.Text, Pphone.Text, regPassword.Text);
                        SqlCommand command = new SqlCommand(strCmd, con);
                        command.ExecuteNonQuery();
                    }
                    MessageBox.Show("注册成功！");
                }
            
            
            }
            

                else if (character.Text.ToString() == "患者")
                {
                    using (SqlConnection con = new SqlConnection(strCon))
                    {
                        con.Open();
                        string strCmd = "insert Patient values('{0}','{1}','{2}','{3}','{4}','{5}','{6}') " +
                        "insert Plogin values('{0}','{7}')";
                        strCmd = string.Format(strCmd, Id.Text, Pname.Text, Psex.Text, Page.Text, Pphone.Text, Idtype.Text, Idnum.Text, regPassword.Text);
                        SqlCommand command = new SqlCommand(strCmd, con);
                        command.ExecuteNonQuery();
                    }
                MessageBox.Show("注册成功！");
            }
            else
            {
                MessageBox.Show("注册失败");
            }

                    
                
                
        }


        private void character_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            Dept_type.Items.Clear();
            if (character.Text == "医生")
            {
                Id.Text = "";

                xDept.Visible = true;
                Dept.Visible = true;
                Dept_type.Visible = true;
                xDept_type.Visible = true;

                using (SqlConnection con = new SqlConnection(strCon))
                {
                    con.Open();
                    string strCmd = "select distinct Dept_type from Doctor";
                    SqlCommand command = new SqlCommand(strCmd, con);
                    SqlDataReader dr = command.ExecuteReader();
                    
                    while (dr.Read())
                    {
                        object[] values = new object[dr.FieldCount];
                        dr.GetValues(values);
                        Dept_type.Items.AddRange(values);
                    }
                    dr.Close();
                    
                    
                }

            }
            else
            {
                
                xDept.Visible = false;
                Dept.Visible = false;
                Dept_type.Visible = false;
                xDept_type.Visible = false;

                using (SqlConnection con = new SqlConnection(strCon))
                {
                    con.Open();
                    string strCmd = "select TOP 1 Card_num from Patient order by Card_num desc";
                    SqlCommand command = new SqlCommand(strCmd, con);
                    SqlDataReader dr = command.ExecuteReader();
                    dr.Read();
                    int id = Convert.ToInt32(dr.GetString(0));
                    dr.Close();
                    Id.Text = (++id).ToString();
                }
            }
        }

 

        private void Page_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                int len = Page.Text.Length;
                if (len < 1 && e.KeyChar == '0')
                {
                    e.Handled = true;
                }
                else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
                {
                    e.Handled = true;
                }
            }
        }

        private void Dept_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dept.Items.Clear();
            using (SqlConnection con = new SqlConnection(strCon))
            {
                con.Open();
                string strCmd = "select TOP 1 Id from Doctor where Dept_type ='{0}' order by Id desc";
                strCmd = string.Format(strCmd, Dept_type.Text);
                SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                int id = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                Id.Text = (++id).ToString();

                //if (string.IsNullOrEmpty(Dept_type.Text))
                //{
                    string str = "select distinct Dept from Dept where Dept_type='{0}'";
                    str = string.Format(str, Dept_type.Text);
                    SqlCommand command = new SqlCommand(str, con);
                    SqlDataReader dr = command.ExecuteReader();

                    while (dr.Read())
                    {
                        object[] values = new object[dr.FieldCount];
                        dr.GetValues(values);
                        Dept.Items.AddRange(values);
                    }
                    dr.Close();
                //}
             




            }
        }



      

        private void Pphone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                int len = Pphone.Text.Length;
                if (len < 1 && e.KeyChar == '0')
                {
                    e.Handled = true;
                }
                else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
                {
                    e.Handled = true;
                }
            }
        }

        private void Idnum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                int len = Idnum.Text.Length;
                if (len < 1 && e.KeyChar == '0')
                {
                    e.Handled = true;
                }
                else if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 
                {
                    e.Handled = true;
                }
            }
        }

  
        private void Pname_KeyPress(object sender, KeyPressEventArgs e)
        {
            Regex rg = new Regex("^[\u4e00-\u9fa5]+$"); //正则表达式

            if (!rg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b') //'\b'是退格键

            {

                e.Handled = true;

            }
        }


    }
}
